﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using DataLayer;

namespace Audit
{
    public static class ModellingOrderFacade
    {
        public static T Detach<T>(T item)
        {
            string detachedProp = Serialize(item);
            return (T)Deserialize(typeof(T), detachedProp);
        }

        private static string Serialize(object value)
        {
            if (value.GetType() == typeof(string))
                return value.ToString();

            StringWriter stringWriter = new StringWriter();
            using (XmlWriter writer = XmlWriter.Create(stringWriter))
            {
                DataContractSerializer serializer = new
                  DataContractSerializer(value.GetType());
                serializer.WriteObject(writer, value);
            }

            return stringWriter.ToString();
        }

        private static object Deserialize(Type type, string serializedValue)
        {
            if (type == typeof(string))
                return serializedValue;

            using (StringReader stringReader = new StringReader(serializedValue))
            {
                using (XmlReader reader = XmlReader.Create(stringReader))
                {
                    DataContractSerializer serializer =
                     new DataContractSerializer((type));

                    object deserializedValue = serializer.ReadObject(reader);

                    return deserializedValue;
                }
            }
        }

        public static Order GetOrder(DateTime date)
        {
            var order = new Order { Date = new Date() { RegisterDate = date }, Client = new Client { Name = "Andrew", Phone = "1234" } };

            var countOfTypes = 0;

            while (countOfTypes < 1 || countOfTypes > 3)
            {
                countOfTypes = DistributionFacade.Instance.GetCountOfPositions() + 1;
            }

            while (order.ProductOrders.Count() < countOfTypes)
            {
                var positionId = DistributionFacade.Instance.GetPosition();

                if (order.ProductOrders.FirstOrDefault(x => x.ProductId == positionId) == null)
                {
                    var countOfProducts = DistributionFacade.Instance.GetCountOfProductsInPosition();
                    var product = Repository.Instance.GetProductById(positionId);
                    order.ProductOrders.Add(new ProductOrder { Product = product, Quantity = countOfProducts });
                }
            }

            return order;
        }


        public static void ChangeOrder(DateTime date, Order order)
        {
            order.Date.UpdatedDate = date;

            var countOfTypes = 0;

            while (countOfTypes < 1 || countOfTypes > 3)
            {
                countOfTypes = DistributionFacade.Instance.GetCountOfPositions() + 1;
            }

            Repository.Instance.DeleteAllProductsOrder(order);

            while (order.ProductOrders.Count() < countOfTypes)
            {
                var positionId = DistributionFacade.Instance.GetPosition();

                if (order.ProductOrders.FirstOrDefault(x => x.ProductId == positionId) == null)
                {
                    var countOfProducts = DistributionFacade.Instance.GetCountOfProductsInPosition();
                    var product = Repository.Instance.GetProductById(positionId);
                    order.ProductOrders.Add(new ProductOrder { Product = product, Quantity = countOfProducts });
                }
            }
        }
    }
}
